﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Xml.Utility;

namespace Portal.Cms.Model
{
    public class DocStatiticsCls
    {
        public string Id;
        public string Subject;
        public int    Total = 0;
    }

    public class DocStatiticsParser
    {
        public static DocStatiticsCls CreateInstance()
        {
            //tao 1 class trang....
            DocStatiticsCls oDocStatiticsCls = new DocStatiticsCls();
            return oDocStatiticsCls;
        }

        public static DocStatiticsCls ParseFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            DocStatiticsCls oDocStatiticsCls = ParseFromDataRow(ds.Tables[0].Rows[0]);
            return oDocStatiticsCls;
        }


        public static DocStatiticsCls ParseFromDataRow(DataRow dr)
        {
            DocStatiticsCls oDocStatiticsCls = new DocStatiticsCls();
            oDocStatiticsCls.Id = XmlUtility.GetString(dr, "Id", true);
            oDocStatiticsCls.Subject = XmlUtility.GetString(dr, "Subject", true);
            oDocStatiticsCls.Total = XmlUtility.GetInt(dr, "Total", true);

            return oDocStatiticsCls;
        }

        public static DocStatiticsCls[] ParseFromDataTable(DataTable dtTable)
        {
            DocStatiticsCls[] DocStatiticss = new DocStatiticsCls[dtTable.Rows.Count];
            for (int iIndex = 0; iIndex < dtTable.Rows.Count; iIndex++)
            {
                DocStatiticss[iIndex] = ParseFromDataRow(dtTable.Rows[iIndex]);
            }

            return DocStatiticss;
        }

        public static DataSet CreateDataset()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add("Info");

            ds.Tables["Info"].Columns.Add("Id");
            ds.Tables["Info"].Columns.Add("Subject");
            ds.Tables["Info"].Columns.Add("Total");
            return ds;
        }

        public static string GetXml(DocStatiticsCls oDocStatiticsCls)
        {
            DataSet ds = CreateDataset();
            ds.Tables["Info"].Rows.Add(new object[]{
                oDocStatiticsCls.Id,
                oDocStatiticsCls.Subject,
                oDocStatiticsCls.Total,
            });
            return ds.GetXml();
        }

        public static string GetXml(DataRow dr)
        {
            DocStatiticsCls oDocStatitics = DocStatiticsParser.ParseFromDataRow(dr);
            string Xml = DocStatiticsParser.GetXml(oDocStatitics);
            return Xml;
        }

        public static string GetXml(DataTable dt)
        {
            if (dt == null) return null;
            DocStatiticsCls[] DocStatiticss = new DocStatiticsCls[dt.Rows.Count];
            for (int iIndex = 0; iIndex < dt.Rows.Count; iIndex++)
            {
                DocStatiticss[iIndex] = DocStatiticsParser.ParseFromDataRow(dt.Rows[iIndex]);
            }
            string Xml = DocStatiticsParser.GetXml(DocStatiticss);
            return Xml;
        }


        public static string GetXml(DocStatiticsCls[] DocStatiticss)
        {
            if (DocStatiticss == null)
            {
                //neu null thi khoi tao = mang rong de cho do loi...!
                DocStatiticss = new DocStatiticsCls[0];
            }
            DataSet ds = CreateDataset();
            for (int iIndex = 0; iIndex < DocStatiticss.Length; iIndex++)
            {
                DocStatiticsCls oDocStatiticsCls = DocStatiticss[iIndex];
                ds.Tables["Info"].Rows.Add(new object[]
                {
                    oDocStatiticsCls.Id,
                    oDocStatiticsCls.Subject,
                    oDocStatiticsCls.Total,
                });
            }
            string XmlData = ds.GetXml();
            return XmlData;
        }

        public static DocStatiticsCls[] ParseMultiRecordFromXml(string XmlData)
        {
            DataSet ds = XmlUtility.GetDataSetFromXml(XmlData);
            if (ds.Tables.Count == 0)
            {
                return new DocStatiticsCls[0];
            }
            DocStatiticsCls[] DocStatiticss = new DocStatiticsCls[ds.Tables[0].Rows.Count];
            for (int iIndex = 0; iIndex < ds.Tables[0].Rows.Count; iIndex++)
            {
                DocStatiticss[iIndex] = ParseFromDataRow(ds.Tables[0].Rows[iIndex]);
            }
            return DocStatiticss;
        }
    }
}
